package com.hspedu.smallchange;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/1/24
 * @time 16:20
 */
@SuppressWarnings("all")
public class SmallChangeSys {
    public static void main(String[] args) {

        boolean flag = true;
        Scanner scan = new Scanner(System.in);
        //零钱通明细
        String detail = "-----------------零钱通明细---------------";

        //收益入账
        double money = 0;
        double balance = 0;
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");

        //消费
        String note = null;


        do {
            System.out.println("\n-----------零钱通菜单-----------");
            System.out.println("\t\t\t1 零钱通明细");
            System.out.println("\t\t\t2 收益入账");
            System.out.println("\t\t\t3 消\t费");
            System.out.println("\t\t\t4 退\t出");
            System.out.println("请选择1-4：");
            char key = scan.next().charAt(0);

            switch (key) {
                case '1':
                    System.out.println(detail);
                    break;
                case '2':
                    boolean flag2 = true;
                    do {
                        System.out.println("请输入收益入账金额：");
                        money = scan.nextDouble();
                        if (money <= 0) {
                            System.out.println("输入不合法,请重新输入，收益应当大于0");
                        } else {
                            flag2 = false;
                            balance += money;
                            date = new Date();
                            detail += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t" + balance;
                        }
                    } while (flag2);
                    break;
                case '3':
                    if (balance == 0){
                        System.out.println("现在余额为0，不能消费");
                        break;
                    }
                    boolean flag3 = true;
                    do {
                        System.out.print("消费金额：");
                        money = scan.nextDouble();
                        if (money > balance) {
                            System.out.println("消费超过余额，请重新输入");
                        } else if (money <= 0) {
                            System.out.println("输入非法，消费金额应该为正数");
                        } else {
                            flag3 = false;
                            balance -= money;
                            System.out.print("消费说明：");
                            note = scan.next();
                            date = new Date();
                            detail += "\n" + note + "\t\t-" + money + "\t" + sdf.format(date) + "\t" + balance;
                        }
                    } while (flag3);
                    break;
                case '4':
                    boolean flag4 = true;
                    do {
                        System.out.println("确认要退出程序？y/n");
                        char a = scan.next().charAt(0);
                        if (a == 'y') {
                            flag = false;
                            flag4 = false;
                            System.out.println("退出程序");
                        } else if (a == 'n') {
                            flag4 = false;
                            System.out.println("回到首页");
                        } else {
                            System.out.println("输入不合法，请重新输入");
                        }
                    } while (flag4);
                    break;
                default:
                    System.out.println("输入有误请重新输入");
                    break;
            }
        } while (flag);


    }
}
